//
//  GradientArcProgressView.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/7/3.
//

#import "GradientArcProgressView.h"

@implementation GradientArcProgressView
{
    CAShapeLayer *_arcLayer;
    CAShapeLayer *_backgroundLayer;
    CAGradientLayer *_gradientLayer;
}

- (instancetype)initWithFrame:(CGRect)frame
               backgroundColor:(UIColor *)backgroundColor
                    startColor:(UIColor *)startColor
                      endColor:(UIColor *)endColor{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = backgroundColor;
        self.startColor = startColor;
        self.endColor = endColor;
        [self setupView];
    }
    return self;
}

- (void)setupView {
    // 创建底部圆环层
    CGFloat diameter = 112.0;
    CGFloat lineWidth = 10.0;
    CGFloat radius = (diameter - lineWidth) / 2.0;
    CGPoint center = CGPointMake(self.bounds.size.width / 2.0, self.bounds.size.height / 2.0);
    UIBezierPath *backgroundPath = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle:M_PI * 2 clockwise:YES];
    
    _backgroundLayer = [CAShapeLayer layer];
    _backgroundLayer.path = backgroundPath.CGPath;
    _backgroundLayer.strokeColor = self.backgroundColor.CGColor;
    _backgroundLayer.fillColor = [UIColor clearColor].CGColor;
    _backgroundLayer.lineWidth = lineWidth;
    _backgroundLayer.lineCap = kCALineCapRound;
    [self.layer addSublayer:_backgroundLayer];
}

- (void)setProgress:(CGFloat)progress {
    _progress = progress;
    
    if (!_arcLayer) {
        // 创建渐变圆弧层
        _arcLayer = [CAShapeLayer layer];
        _arcLayer.path = [self circularPathWithProgress:1.0].CGPath;
        _arcLayer.strokeColor = [UIColor blackColor].CGColor;
        _arcLayer.fillColor = [UIColor clearColor].CGColor;
        _arcLayer.lineWidth = 10.0;
        _arcLayer.lineCap = kCALineCapRound;
        _arcLayer.strokeStart = 0.0;
        _arcLayer.strokeEnd = self.progress;
        
        // 创建渐变层
        _gradientLayer = [CAGradientLayer layer];
        _gradientLayer.frame = self.bounds;
        _gradientLayer.colors = @[
            (__bridge id)self.startColor.CGColor,
            (__bridge id)self.endColor.CGColor
        ];
        _gradientLayer.startPoint = CGPointMake(0.5, 0.0);
        _gradientLayer.endPoint = CGPointMake(0.5, 1.0);
        [_gradientLayer setMask:_arcLayer];
        
        [self.layer addSublayer:_gradientLayer];
    }
    
    _arcLayer.strokeEnd = self.progress;
    
    if (self.progress == 1.0) {
        _gradientLayer.colors = @[(__bridge id)self.endColor.CGColor, (__bridge id)self.endColor.CGColor];
    } else {
        _gradientLayer.colors = @[
            (__bridge id)self.startColor.CGColor,
            (__bridge id)self.endColor.CGColor
        ];
        
        // 计算 endPoint based on progress
        CGFloat angle = M_PI * 2 * self.progress;
        CGFloat x = cos(angle - M_PI_2);
        CGFloat y = sin(angle - M_PI_2);
        _gradientLayer.endPoint = CGPointMake(0.5 + x * 0.5, 0.5 + y * 0.5);
    }
    
    [self setNeedsDisplay];
}


- (UIBezierPath *)circularPathWithProgress:(CGFloat)progress {
    CGFloat diameter = 112.0;
    CGFloat lineWidth = 10.0;
    CGFloat radius = (diameter - lineWidth) / 2.0;
    CGPoint center = CGPointMake(self.bounds.size.width / 2.0, self.bounds.size.height / 2.0);
    CGFloat startAngle = -M_PI_2;
    CGFloat endAngle = startAngle + (M_PI * 2 * progress);
    return [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
}


@end






